package com.atl.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
//    @Bean
//    public Docket createRestApi() {
//        // 指定api类型为swagger2
//        return new Docket(DocumentationType.SWAGGER_2)
//                // 用于定义api文档汇总信息
//                .apiInfo(apiInfo())
//                .groupName("基本框架")
////                .globalRequestParameters(parameters)
//                .pathMapping("/")
//                .select()
//                // 指定路径处理PathSelectors.any()代表所有的路径
//                .paths(PathSelectors.any()).build();
//    }
//
//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                .title("共享充电桩")
//                .version("1.0.0")
//                .description("共享充电桩")
//                .build();
//    }

    /**
     * 通过knife4j生成接口文档
     *
     * @return
     */
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理员")
                .apiInfo(apiInfo("共享充电桩管理端接口文档", "共享充电桩接口文档"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.atl.controller.web"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("代理/商家")
                .apiInfo(apiInfo("共享充电桩代理端文档", "共享充电桩接口文档"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.atl.controller.wx.admin"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户")
                .apiInfo(apiInfo("共享充电桩用户端文档", "共享充电桩接口文档"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.atl.controller.wx.user"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(String title, String description) {
        return new ApiInfoBuilder()
                .title(title)
                .version("1.0")
                .description(description)
                .build();
    }


    //让SpringSecurity框架放行swagger接口
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }


}
